package org.jcms.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jcms.model.Book;
import org.jcms.model.Manager;
import org.jcms.model.Order;
import org.jcms.model.Proof;
import org.jcms.model.Proofstate;
import org.jcms.model.State;
import org.jcms.service.BookServiceI;
import org.jcms.service.ManagerServiceI;
import org.jcms.service.OrderBookServiceI;
import org.jcms.service.OrderServiceI;
import org.jcms.service.ProofServiceI;
import org.jcms.service.ProofstateServiceI;
import org.jcms.service.StateServiceI;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 发书：参数 proofId
 * 
 * @author Administrator
 * 
 */
public class ManagerSellBook extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ManagerSellBook() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		OrderServiceI orderServiceI = (OrderServiceI) ac
				.getBean("orderServiceImpl");
		StateServiceI stateServiceI = (StateServiceI) ac
				.getBean("stateServiceImpl");
		ManagerServiceI managerServiceI = (ManagerServiceI) ac
				.getBean("managerServiceImpl");
		ProofstateServiceI proofstateServiceI = (ProofstateServiceI) ac
				.getBean("proofstateServiceImpl");
		ProofServiceI proofServiceI = (ProofServiceI) ac
				.getBean("proofServiceImpl");
		OrderBookServiceI orderBookServiceI = (OrderBookServiceI) ac
				.getBean("orderBookServiceImpl");
		BookServiceI bookServiceI = (BookServiceI) ac
				.getBean("bookServiceImpl");
		Manager manager = (Manager) request.getSession().getAttribute(
				"manager-0");
		Manager manager2 = (Manager) request.getSession().getAttribute(
				"manager-1");
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		String proofId = request.getParameter("proofId");
		try {
			if (manager == null && manager2 == null) {
				msg = "网页已过期";
				return;
			}
			if (manager == null) {
				msg = "只有教材发行人员才有该操作权限";
				return;
			}
			if (proofId == null || "".equals(proofId)) {
				msg = "领书单ID不能为空";
				return;
			}
			Proof proof = proofServiceI.findById(proofId);
			if (proof == null) {
				msg = "要操作的领书单不存在";
				return;
			}

			if (proof.getProofstate().getProofStateId() != 0) {
				msg = "该领书单已完结，不能再次领书";
				return;
			}
			Order order = orderServiceI.findById(proof.getOrder().getOrderId());
			if (order.getState().getStateId() != 2) {
				msg = "该领书单的订单状态为：" + order.getState().getStateName()
						+ ",不能完成领书操作";
				return;
			}
			State state = stateServiceI.findById(3);
			order.setState(state);
			Proofstate proofstate = proofstateServiceI.findById(1);
			proof.setProofstate(proofstate);
			try {
				managerServiceI.sellBook(order, proof, orderBookServiceI);
				err = "0";
				msg = "领书单(" + proofId + ")领书成功";
			} catch (RuntimeException re) {
				String isbn = re.getMessage();
				Book book = bookServiceI.findById(isbn);
				if (book != null) {
					msg = "教材(" + isbn + "),书名(" + book.getBookName()
							+ ")库存不足，该领书单不能完成领书操作";
				} else {
					msg = "系统出错";
				}
			}

		} catch (Exception e) {
			msg = "系统出错";
		} finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	public void init() throws ServletException {

	}

}
